package com.webdev.base.controller;


import com.webdev.base.pojo.Admin;
import com.webdev.base.pojo.Goods;
import com.webdev.base.pojo.User;
import com.webdev.base.service.IGoodsService;
import com.webdev.base.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author huangningshuang
 * @since 2021-12-13
 */
@Controller
@RequestMapping("/goods")
@Slf4j
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @GetMapping("/hello")
    @ResponseBody
    public RespBean hello(User user) {
        return RespBean.success(user);
    }

    @GetMapping(value = "/list")
    @ResponseBody
    public RespBean toList() {
        List<GoodsVo> goodsVoList = goodsService.findGoodsVoList();
        return RespBean.success(goodsVoList);
    }

    /**
     * 跳转商品详情页面
     *
     * @param goodId
     * @return
     */
    @RequestMapping("/detail/{goodId}")
    @ResponseBody
    public RespBean toDetail(User user, @PathVariable Integer goodId) throws ParseException {
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodId(goodId);
        log.info(goodsVo.toString());
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        log.info(startDate + ":" + endDate);
        //是否还再秒杀时间状态 0未开始，1进行中，2已结束
        int secKillStatus = 0;
        //秒杀倒计时
        int remainSeconds = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); // 设置北京时区
        Date cur = sdf.parse(sdf.format(new Date()));
        log.info("cur time: " + cur);
        if (cur.before(startDate)) {
            //秒杀还没开始
            remainSeconds = ((int) (startDate.getTime() - cur.getTime()) / 1000);
        } else if (cur.after(endDate)) {
            //秒杀已结束
            secKillStatus = 2;
        } else {
            //秒杀进行中
            secKillStatus = 1;
        }

        GoodsDetailVo goodDetailVo = new GoodsDetailVo();
        goodDetailVo.setGoodsVo(goodsVo);
        goodDetailVo.setSecKillStatus(secKillStatus);
        goodDetailVo.setRemainSeconds(remainSeconds);

        return RespBean.success(goodDetailVo);
    }

    @ResponseBody
    @GetMapping("/delete/{goodsId}")
    public RespBean deleteGoods(Admin admin, @PathVariable Integer goodsId) {
        if (admin == null) return RespBean.error(RespBeanEnum.PRIVILEDGE_ERROR);
        Integer row = goodsService.deleteGoodsById(goodsId);
        if (row == 0) return RespBean.error(RespBeanEnum.GOOD_DELETE_ERROR);
        return RespBean.success();
    }

    @ResponseBody
    @PostMapping("/create")
    public RespBean insertGoods(Admin admin, @RequestBody GoodsVo goodsVo) {
        if (admin == null) return RespBean.error(RespBeanEnum.PRIVILEDGE_ERROR);
        log.info("goodsVo = " + goodsVo);
        goodsService.insertGoods(goodsVo);
        return RespBean.success();
    }

    @ResponseBody
    @PostMapping("/update/{goodsId}")
    public RespBean updateGoods(Admin admin, @PathVariable Integer goodsId, @RequestBody GoodsVo goodsVo) {
        if (admin == null) return RespBean.error(RespBeanEnum.PRIVILEDGE_ERROR);
        log.info("goodsVo = " + goodsVo);
        Integer row = goodsService.updateById(goodsId, goodsVo);
        if (row == 1) return RespBean.success();
        return RespBean.error(RespBeanEnum.GOOD_UPDATE_ERROR);
    }
}
